package ali;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.alibaba.ttl.TransmittableThreadLocal;
import com.alibaba.ttl.threadpool.TtlExecutors;

/**
 *
 * @author wxh
 */
public class Solution_14 {

	public boolean isValid(String s) {
		if (s.length() % 2 == 1) { //非偶数，直接返回false
			return false;
		}

		Map<Character, Character> map = new HashMap<Character, Character>() {{
			put('(', ')');
			put('[', ']');
			put('{', '}');
		}};

		Stack<Character> stack = new Stack();
		for (int i = 0; i < s.length(); i++) {
			char ch = s.charAt(i);
			if (map.containsKey(ch)) { //如果是左括号，直接入栈
				stack.push(ch);
			} else { //如果是右括号，判断栈顶的左括号，其对应的右括号，是否等于当前的符号
				if (stack.isEmpty() || ch != map.get(stack.peek())) {
					return false; //不匹配，直接结束返回false
				}
				stack.pop(); // 匹配，则出栈
			}
		}
		return stack.isEmpty();
	}

	public static void main(String[] args) throws Exception {
		ThreadLocal<String> threadLocal = new TransmittableThreadLocal<>();

		ExecutorService threadPool = TtlExecutors.getTtlExecutorService(Executors.newFixedThreadPool(2));
		for (int i = 0; i < 5; i++) {
			threadLocal.set("初始化的值能继承吗？" + i);
			System.out.println("父线程的ThreadLocal值：" + threadLocal.get());
			threadPool.execute(new Runnable() {
				@Override
				public void run() {
					System.out.println("子线程到了");
					System.out.println("=========子线程的ThreadLocal值：" + threadLocal.get());
				}
			});
		}
	}

}